﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;

namespace worktime
{
	class ErrorHandler
	{
		private void WriteToLog(string text)
		{
			if (string.IsNullOrEmpty(text))
				return;

			try
			{
				using (StreamWriter sw = new StreamWriter(Path.GetDirectoryName(Program.DatabaseFullFileName) + "\\worktime.log", true, Encoding.Unicode))
				{
					text = text.Replace("\r\n", " / ");
					text = text.Replace("\r", " / ");
					text = text.Replace("\n", " / ");

					sw.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString(), text));
					sw.Close();
				}
			}
			catch
			{
			}
		}

		private void ShowMessage(string text)
		{
			lock (Program.ErrorShownSaver)
			{
				Program.ErrorIsShown = true;
			};

			MessageBox.Show(text, "WorkTime Error");

			lock (Program.ErrorShownSaver)
			{
				Program.ErrorIsShown = false;
			};
		}

		public void ProcessSeriousError(string text)
		{
			WriteToLog(text);
			ShowMessage(text);
		}

		public void ProcessNotSeriousError(string text)
		{
			WriteToLog(text);
		}
	}
}
